package test.multithreading;

/**
 * @Project : algorithm-learning-java
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/3/11 16:00
 */
public class ThreeThreads {
    public static final Object lock = new Object();
    public static volatile int x = 0;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (x < 100) {
                if (x % 3 == 0) {
                    synchronized (lock) {
                        System.out.println("Thread1:" + x++);
                    }
                }
            }
        });
        Thread t2 = new Thread(() -> {
            while (x < 100) {
                if (x % 3 == 1) {
                    synchronized (lock) {
                        System.out.println("Thread2:" + x++);
                    }
                }
            }
        });
        Thread t3 = new Thread(() -> {
            while (x < 100) {
                if (x % 3 == 2) {
                    synchronized (lock) {
                        System.out.println("Thread3:" + x++);
                    }
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
